package com.fhz.shop.ui.search;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fhz.shop.R;
import com.fhz.shop.databinding.ActivitySearchBinding;
import com.fhz.shop.entity.SearchBean;

import java.util.ArrayList;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class SearchActivity extends AppCompatActivity {

    SearchViewModel mSearchViewModel;
    ActivitySearchBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivitySearchBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mSearchViewModel = new ViewModelProvider(this).get(SearchViewModel.class);

        binding.searchRv.setLayoutManager(new LinearLayoutManager(this){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        List<SearchBean> list = new ArrayList<>();

        SearchRVAdapter searchRVAdapter = new SearchRVAdapter(list);
        binding.searchRv.setAdapter(searchRVAdapter);

        binding.searchView.setOnClickListener( v -> {
            SearchBean daming = new SearchBean("大明明", 0);
            mSearchViewModel.insert(daming);

            //
            List<SearchBean> searchBeans = mSearchViewModel.queryAll();
            System.out.println(searchBeans.size());
            list.clear();
            list.addAll(searchBeans);
            searchRVAdapter.notifyDataSetChanged();

        });

        searchRVAdapter.addOnItemChildClickListener(R.id.search_item_del, (baseQuickAdapter, view, i) -> {

            SearchBean searchBean = list.get(i);
            mSearchViewModel.delete(searchBean);
            list.remove(i);
            searchRVAdapter.notifyDataSetChanged();
        });



    }

    public static void launch(Context context){
        context.startActivity(new Intent(context,SearchActivity.class));
    }

}